package today.rocky.memory.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import today.rocky.memory.common.CommonResult;
import today.rocky.memory.data.PhantonInspire;
import today.rocky.memory.data.PhantonQuote;
import today.rocky.memory.domain.Member;
import today.rocky.memory.domain.bo.*;
import today.rocky.memory.domain.dto.MemberDTO;
import today.rocky.memory.domain.dto.MemoryRecordAddDTO;
import today.rocky.memory.domain.dto.MemoryRecordDTO;
import today.rocky.memory.exception.BusinessException;
import today.rocky.memory.service.MemberService;
import today.rocky.memory.service.MemoryRecordService;

import javax.validation.Valid;
import java.util.*;

/**
 * @author Rocky
 * @version 1.0
 * @date 2020/5/22 13:34
 * @description
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/today")
public class DiaryController extends BaseController{

	@Autowired
	private MemoryRecordService memoryRecordService;

	@Autowired
	private MemberService memberService;

	@RequestMapping("/getInspire")
	public CommonResult getInspire() throws BusinessException {
		String inspire = new PhantonInspire().inspires.get((int)Math.round(Math.random()*10));
		log.info("inspire:"+inspire);
		return CommonResult.success(inspire);
	}

	@PostMapping("/insertMemory")
	public CommonResult insertMemory(@RequestBody MemoryRecordDTO memory) throws BusinessException {
		int i = memoryRecordService.saveRecord(memory);
		if(i!=1){
			CommonResult.failed("新增失败");
		}
		Map<String,Object> map = new HashMap<>(8);
		map.put("flag",0);
		map.put("msg","上传成功");
		map.put("memory",memory);
		return CommonResult.success(map);
	}

	@RequestMapping("/getQuote")
	public CommonResult getQuote() throws BusinessException {
		String quote = new PhantonQuote().quote.get((int)Math.round(Math.random()*10));
		log.info("quote:"+quote);
		Map<String,String> map = new HashMap<>(8);
		map.put("flag","0");
		map.put("msg","成功获取名人名言");
		map.put("quote",quote);
		return CommonResult.success(map);
	}

	@PostMapping("/getMemoryList")
	public CommonResult getMemoryList(@RequestBody MemorySearchParam params){
		List<MemoryRecordDTO> records = memoryRecordService.searchMemoryList(params);
		Map<String,Object> map = new HashMap<>(8);
		map.put("flag","0");
		map.put("msg","成功记忆列表名言");
		map.put("beanList",records);
		map.put("length",records.size());
		return CommonResult.success(map);
	}

	@PostMapping("/getMemory")
	public CommonResult getMemory(@RequestBody MemoryParam param) throws BusinessException {
		MemoryRecordDTO record = memoryRecordService.getMemory(param);
		Map<String,Object> map = new HashMap<>(8);
		map.put("flag","0");
		map.put("msg","成功获取记忆");
		map.put("memory",record);
		return CommonResult.success(map);
	}

	@PostMapping("/userRegistry")
	public CommonResult userRegistry(@RequestBody @Validated RegistryParam param)throws BusinessException{
		int i = memberService.userRegistry(param);
		if(i!=1){
			throw new BusinessException("注册异常");
		}
		Map<String,Object> map = new HashMap<>(8);
		map.put("flag",0);
		map.put("msg","注册成功");
		return CommonResult.success(map);
	}

	@PostMapping("/userLogin")
	public CommonResult userLogin(@RequestBody @Validated LoginParam param)throws BusinessException{
		Member member = memberService.userLogin(param);
		if(member == null){
			throw new BusinessException("账号不存在");
		}
		MemberDTO memberDTO = new MemberDTO();
		BeanUtils.copyProperties(member,memberDTO);
		log.info(member.toString());
		Map<String,Object> map = new HashMap<>(8);
		map.put("flag",0);
		map.put("msg","登录成功");
		map.put("user",memberDTO);
		return CommonResult.success(map);
	}

	@PostMapping("/updateUserIcon")
	public CommonResult updateUserIcon(@RequestBody @Validated MemberDTO param)throws BusinessException{
		int i = memberService.updateUserIcon(param);
		if(i!=1){
			throw new BusinessException("头像上传失败");
		}
		Map<String,Object> map = new HashMap<>(8);
		map.put("flag",0);
		map.put("msg","头像更换成功成功");
		return CommonResult.success(map);
	}

	@PostMapping("/refreshPersonData")
	public CommonResult refreshPersonData(@RequestBody @Valid RefreshParam param)throws BusinessException{
		Member member = memberService.refreshMemberData(param);
		MemberDTO memberDTO = new MemberDTO();
		BeanUtils.copyProperties(member,memberDTO);
		Map<String,Object> map = new HashMap<>(8);
		map.put("flag",0);
		map.put("msg","刷新成功");
		map.put("user",memberDTO);
		return CommonResult.success(map);
	}

	@PostMapping("/saveUserFeedback")
	public CommonResult saveUserFeedback(@RequestBody @Valid FeedBackParam param)throws BusinessException{
		int i = memberService.saveFeedBack(param);
		if(i!=1){
			throw new BusinessException("反馈提交失败");
		}
		Map<String,Object> map = new HashMap<>(8);
		map.put("flag",0);
		map.put("msg","反馈提交成功");
		return CommonResult.success(map);
	}


	@PostMapping("/saveInspire")
	public CommonResult saveInspire(@RequestBody @Valid InspireParam param)throws BusinessException{
		int i = memberService.saveInspire(param);
		if(i!=1){
			throw new BusinessException("灵感提交失败");
		}
		Map<String,Object> map = new HashMap<>(8);
		map.put("flag",0);
		map.put("msg","灵感提交成功");
		return CommonResult.success(map);
	}
	@PostMapping("/saveQuote")
	public CommonResult saveQuote(@RequestBody @Valid QuoteParam param)throws BusinessException{
		int i = memberService.saveQuote(param);
		if(i!=1){
			throw new BusinessException("灵感提交失败");
		}
		Map<String,Object> map = new HashMap<>(8);
		map.put("flag",0);
		map.put("msg","灵感提交成功");
		return CommonResult.success(map);
	}

}
